﻿/*******************************************************************************
 **  Name:                                        **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                            **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Created on November 1, 2010, 7:25 AM
// Modified $Date$ by $Author$
// $Id$

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace sat1baf1
{
    public class Origin
    {
        private string _name;
        private string _desc;
        private string[] _benefits;

        public Origin()
        {
            _name = "";
            _desc = "";
            _benefits = new string[5];
        }
        public Origin(string s, string d) { _name = s; _desc = d; _benefits = new string[5]; }
        public void AddBenefit(string b)
        {
            if (_benefits.Count() > 0)
            {
                _benefits[_benefits.Count()] = b;
            }
            else
                _benefits[0] = b;
        }
        public string Name { get { return _name; } set { _name = value; } }
        public string Desc { get { return _desc; } set { _desc = value; } }
        public string[] Benefits { get { return _benefits; } }
    }

    public class OTalent : Origin
    {
        OTAttr[] _attrs;

        public OTalent()
        {
            this._attrs = new OTAttr[2];
        }
        public void AddAttr(sbyte m, string a)
        {
            OTAttr newattr = new OTAttr(m,a);
            if (_attrs.Count() > 0)
            {
                _attrs[_attrs.Count()] = newattr;
            }
            else
                _attrs[0] = newattr;
        }

        public OTAttr [] Attrs { get { return _attrs; } }
        public class OTAttr
        {
            sbyte mod;
            string attr;
            public OTAttr()
            {
                mod = 0;
                attr = "NONE";
            }
            public OTAttr(sbyte m, string a) { mod = m; attr = a; }
            public sbyte Mod { get { return mod; } set { mod = value; } }
            public string Attr { get { return attr; } set { attr = value; } }

        }
    }
    public class OSpecialty : Origin
    {
        string _feat;
        public OSpecialty()
        {

        }
        public string Feat { get { return _feat; } }
    }

    public sealed class OriginDB
    {
        static string _fileName;
        XElement _x;

        static OriginDB _instance;
        public static OriginDB Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new OriginDB();
                return _instance;
            }
        }

        private OriginDB()
        {
            _fileName = @"Data Files\Origins.xml";
            _x = XElement.Load(_fileName); // Root element
            XmlLog.Instance.Add("Loaded OriginDB log from " + _fileName);
        }

        // TODO
        public Origin GetOrigin(string name)
        {
            IEnumerable<XElement> org =
                from el in _x.Elements("Talents").Elements("Talent")
                where (string)el.Element("Name") == name
                select el;
            Origin newOrigin;
            if (org.Count() == 0)
            {
                // Do the look in Specialties
                org =
                from el in _x.Elements("Specialties").Elements("Specialty")
                where (string)el.Element("Name") == name
                select el;
                newOrigin = new OSpecialty();
                foreach (XElement el in org)
                {

                }


            }
            else
            {
                newOrigin = new OTalent();
                foreach (XElement el in org)
                {

                }

            }
            return newOrigin;
        }
        // TODO
        public IEnumerable<XElement> FindTalentByName(string name)
        {
            // The event arg name is the name of the node
#if DEBUG
            XmlLog.Instance.Add("FindTalentByName: Looking for <Name>" + name + "</Name>");

#endif
            IEnumerable<XElement> org =
                from x in _x.Elements("Talents")
                where (string)x.Element("Name") == name
                select x;

            return org;
        }
        public IEnumerable<XElement> FindSpecialtyByName(string name)
        {
            // The event arg name is the name of the node
#if DEBUG
            XmlLog.Instance.Add("FindSpecialtyByName: Looking for <Name>" + name + "</Name>");

#endif
            IEnumerable<XElement> org =
                from x in _x.Elements("Specialties")
                where (string)x.Element("Name") == name
                select x;

            return org;
        }
        public IEnumerable<XElement> FindOriginByNode(string node)
        {
            // The event arg name is the name of the node
#if DEBUG
            XmlLog.Instance.Add("FindOriginByNode: Looking for <NAME id=\" " + node + "\">");

#endif
            IEnumerable<XElement> org =
                from x in _x.Elements("Talents")
                where (string)x.Element("Name") == node
                select x;

            return org;
        }

        public void Add(XElement newOrigin)
        {
            _x.Add(newOrigin);
            _x.Save(_fileName);
        }

        public void Clear()
        {
            _x.RemoveNodes();
            _x.Save(_fileName);
        }


        // Properties
        // Read-only
        public XElement Origins { get { return _x; } }
        public IEnumerable<XElement> Talents { get { return _x.Elements("Talents"); } }
        public IEnumerable<XElement> Specialties { get { return _x.Elements("Specialties"); } }

    }

}
